<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/28
 * Time: 9:49
 */

namespace app\admin\controller;
use app\admin\model\MemberUser;
use app\admin\model\ProfessClassify;
use app\admin\model\ProfessLunbo;
use app\admin\model\ProfessAdvert;
use app\admin\model\ProfessForum;
use app\admin\model\ProfessForumReply;

class Profess extends Base
{
    public function professClassifyList(){
        return $this->fetch();
    }
    public function professClassifyListAjax($page,$limit){
        $data= ProfessClassify::page($page,$limit)->select();
        $count = ProfessClassify::count();
        $result = [
            'code'=>0,
            'msg'=>'返回成功',
            'count'=>$count,
            'data'=>$data
        ];
        return json($result);
    }
    public function professClassifyAdd(){
        return $this->fetch();
    }
    public function professClassifyAddFrom(){
        $post=input('post.');
        $data=[
            'cl_name'=>$post['cl_name'],
            'cl_logo'=>$post['cl_logo'],
            'cl_image'=>$post['cl_image'],
            'cl_url'=>$post['cl_url'],
            'cl_sort'=>$post['cl_sort']
        ];
        $save= ProfessClassify::insert($data);
        if($save){
            return true;
        }
        return json(['msg'=>'提交失败！']);
    }
    public function professClassifyEdit(){
        $whereAA['cl_id']=input('get.cl_id');
        $data= ProfessClassify::where($whereAA)->find();
        $this->assign('data',$data);

        return $this->fetch();
    }
    public function professClassifyEditFrom(){
        $post=input('post.');
        $data=[
            'cl_name'=>$post['cl_name'],
            'cl_logo'=>$post['cl_logo'],
            'cl_image'=>$post['cl_image'],
            'cl_url'=>$post['cl_url'],
            'cl_sort'=>$post['cl_sort']
        ];
        $where['cl_id']=$post['cl_id'];
        $save= ProfessClassify::where($where)->update($data);
        if($save){
            return true;
        }
        return json(['msg'=>'提交失败！']);
    }
    public function professClassifyDel(){
        $post=input('post.');
        $count=count($post['cl_id']);
        for($i=0;$i<$count;$i++){
            $whereAA['cl_id']=$post['cl_id'][$i];
            $ret = ProfessClassify::where($whereAA)->delete();
        }
        if (!$ret){
            return json(['code'=>0,'msg'=>'删除失败']);
        }
        return json(['code'=>1,'msg'=>'删除成功']);
    }
    public function shopImage(){
        $file =request()->file('file');
        $ret = $this->upload($file);
        return json($ret);
    }
    public function lunboList(){
        $lunboRes=ProfessLunbo::order('lun_sort desc')->paginate(10);
        $this->assign('data',$lunboRes);
        return $this->fetch();
    }
    public function lunboAdd(){
        return $this->fetch();
    }
    public function lunboAddForm(){
        $post=input('post.');
        $data=[
            'lun_sort'=>$post['lun_sort'],
            'lun_image'=>$post['lun_image'],
            'lun_url'=>$post['lun_url']
        ];
        $save=ProfessLunbo::insert($data);
        if ($save){
            return true;
        }
        return array('msg'=>'提交失败！');
    }
    public function lunboEdit(){
        $whereAA['lun_id']=input('get.lun_id');
        $lunboRes=ProfessLunbo::where($whereAA)->find();
        $this->assign('data',$lunboRes);
        return $this->fetch();
    }
    public function lunboEditForm(){
        $post=input('post.');
        $data=[
            'lun_sort'=>$post['lun_sort'],
            'lun_image'=>$post['lun_image'],
            'lun_url'=>$post['lun_url']
        ];
        $where['lun_id']=$post['lun_id'];
        $save=ProfessLunbo::where($where)->update($data);
        if ($save){
            return true;
        }
        return array('msg'=>'提交失败！');
    }
    public function lunboDel(){
        $post=input('post.');
        $count=count($post['data']);
        for($i=0;$i<$count;$i++){
            $whereAA['lun_id']=$post['data'][$i];
            $del=ProfessLunbo::where($whereAA)->delete();
        }
        if($del){
            return array('msg'=>'success');
        }else{
            return array('msg'=>'error');
        }
    }
    public function advertList(){
        $data=ProfessAdvert::order('v_sort desc')->paginate(10);
        $this->assign('data',$data);
        return $this->fetch();
    }
    public function advertAdd(){
        return $this->fetch();
    }
    public function advertAddForm(){
        $post=input('post.');
        $data=[
            'v_logo'=>$post['v_logo'],
            'v_url'=>$post['v_url'],
            'v_sort'=>$post['v_sort']
        ];
        $save=ProfessAdvert::insert($data);
        if ($save){
            return true;
        }
        return array('msg'=>'提交失败！');
    }
    public function advertEdit(){
        $whereAA['v_id']=input('get.v_id');
        $data=ProfessAdvert::where($whereAA)->find();
        $this->assign('data',$data);
        return $this->fetch();
    }
    public function advertEditForm(){
        $post=input('post.');
        $data=[
            'v_logo'=>$post['v_logo'],
            'v_url'=>$post['v_url'],
            'v_sort'=>$post['v_sort']
        ];
        $whereAA['v_id']=$post['v_id'];
        $save=ProfessAdvert::where($whereAA)->update($data);
        if ($save){
            return true;
        }
        return array('msg'=>'提交失败！');
    }
    public function advertDel(){
        $post=input('post.');
        $count=count($post['data']);
        for($i=0;$i<$count;$i++){
            $whereAA['v_id']=$post['data'][$i];
            $del=ProfessAdvert::where($whereAA)->delete();
        }
        if($del){
            return array('msg'=>'success');
        }else{
            return array('msg'=>'error');
        }
    }
    //文章列表
    public function forumList(){
        return $this->fetch();
    }
    public function forumListAjax($page,$limit){
        $data= ProfessForum::page($page,$limit)->order('pf_createtime desc')->select();
        foreach($data as $k=>$v){
            $data[$k]['pf_createtime']=date('Y-m-d H:i',$v['pf_createtime']);

            $whereAB['id']=$v['user_id'];
            $data[$k]['user']=MemberUser::where($whereAB)->find();
            $data[$k]['nick_name']=$data[$k]['user']['nick_name'];
            $data[$k]['image']=$data[$k]['user']['image'];

            $whereAC['pf_id']=$v['id'];
            $data[$k]['repayCount']=ProfessForumReply::where($whereAC)->count();
        }
        $count = ProfessForum::count();
        $result = [
            'code'=>0,
            'msg'=>'返回成功',
            'count'=>$count,
            'data'=>$data
        ];
        return json($result);
    }
    public function forumEdit(){
        $whereAA['id']=input('get.id');
        $data=ProfessForum::where($whereAA)->find();
        $data['pf_image'] = explode(',',$data['pf_image']);
        $this->assign('data',$data);
        return $this->fetch();
    }
    public function forumEditForm(){
        $post=input('post.');
        $data=[
            'pf_sort'=>$post['pf_sort'],
            'pf_image'=>implode(',',$post['pf_image']),
            'pf_title'=>$post['pf_title'],
            'pf_content'=>$post['pf_content']
        ];
        $whereAA['id']=$post['id'];
        $save=ProfessForum::where($whereAA)->update($data);
        if ($save){
            return true;
        }
        return array('msg'=>'提交失败！');
    }
    public function forumDel(){
        $post=input('post.');
        $count=count($post['id']);
        for($i=0;$i<$count;$i++){
            $whereAA['id']=$post['id'][$i];
            $del=ProfessForum::where($whereAA)->delete();
        }
        if($del){
            return array('msg'=>'success');
        }else{
            return array('msg'=>'error');
        }
    }
    //回复列表
    public function forumListReply(){
        if(empty(input('get.bfr_id'))){
            $this->assign('bfr_id',0);
        }else{
            $this->assign('bfr_id',input('get.bfr_id'));
        }
        $this->assign('id',input('get.id'));
        return $this->fetch();
    }
    public function forumListReplyAjax($bfr_id,$pf_id,$page,$limit){
        if($bfr_id==0){
            $whereAA['fid']=0;
        }else{
            $whereAA['bfr_id']=$bfr_id;
        }
        $data= ProfessForumReply::where('pf_id',$pf_id)->where($whereAA)->page($page,$limit)->select();
        foreach($data as $k=>$v){
            $data[$k]['bfr_createtime']=date('Y-m-d H:i',$v['bfr_createtime']);

            $whereAB['id']=$v['user_id'];
            $data[$k]['user']=MemberUser::where($whereAB)->find();
            $data[$k]['nick_name']=$data[$k]['user']['nick_name'];
            $data[$k]['image']=$data[$k]['user']['image'];
        }
        $count = ProfessForumReply::where('pf_id',$pf_id)->where($whereAA)->count();
        $result = [
            'code'=>0,
            'msg'=>'返回成功',
            'count'=>$count,
            'data'=>$data
        ];
        return json($result);
    }
    public function forumReplyEdit(){
        $whereAA['bfr_id']=input('get.bfr_id');
        $data= ProfessForumReply::where($whereAA)->find();
        $this->assign('data',$data);
        return $this->fetch();
    }
    public function forumReplyEditForm(){
        $post=input('post.');
        $data=[
            'bfr_title'=>$post['bfr_title']
        ];
        $whereAA['bfr_id']=$post['bfr_id'];
        $save=ProfessForumReply::where($whereAA)->update($data);
        if ($save){
            return true;
        }
        return array('msg'=>'提交失败！');
    }
    public function forumReplyDel(){
        $post=input('post.');
        $count=count($post['bfr_id']);
        for($i=0;$i<$count;$i++){
            $whereAA['bfr_id']=$post['bfr_id'][$i];
            $del=ProfessForumReply::where($whereAA)->delete();
        }
        if($del){
            return array('msg'=>'success');
        }else{
            return array('msg'=>'error');
        }
    }



}